$(function () {
    var tbody=window.document.getElementById("tbody-result");
    $.ajax({
        type:'GET',
        dataType:'json',
        url:contextPath+"all/doc",
        success:function (data) {
            // console.log(data.count);
            // console.log(data);
            var str="";
            // var l=data.message.length();
            // console.log(l);
            for (var i=0;i<data.count;i++) {
                // console.log(data.list[i].course_name);
                str += "<tr>" +
                    "<td>" + i + "</td>" +
                    "<td>" + data.list[i].course_name+"</td>"+
                    "<td>" + data.list[i].course_dir+ "</td>" +
                    "<td>" + data.list[i].url + "</td>" +
                    "<td>" + data.list[i].nandu + "</td>" +
                    "<td><button onclick='add(this)'>添加到我的文档</button></td>" +
                    "</tr>";
            }
            // console.log(str);
            tbody.innerHTML=str;
        }
        ,
        error:function () {
            alert("查询失败")
        }
    })
});
function add(r) {
    var table=window.document.getElementById("tbody-result");
    var rorNums=table.rows.length;
    var i=r.parentNode.parentNode.rowIndex-1;
    var ss=new Object();
    ss.course_name=table.rows[i].cells[1].innerHTML;
    // console.log(ss.course_name);
    $.ajax({
        type:'PUT',
        dataType:'json',
        contentType: "application/json; charset=utf-8",
        url:contextPath+"learner/newdoc",
        data:JSON.stringify(ss),
        success:function (data) {
            // window.location.href='Student/uservideoQuery.html';
            alert(data.message);
        },
        error:function () {
            alert("添加失败");
        }
    })
}
$('#btn').on('click',function () {
    var table=document.getElementById("tbody-result");
    var ss=new Object();
    ss.course_dir=document.getElementById("menuname").value;
    $.ajax({
        type:'GET',
        url:contextPath+"all/doc/"+ss.course_dir,
        success:function (data) {
            var str="";
            // console.log(data);
            for (var i=0;i<data.count;i++) {
                str += "<tr>" +
                    "<td>" + i + "</td>" +
                    "<td>" + data.list[i].course_name + "</td>" +
                    "<td>" + data.list[i].course_dir + "</td>" +
                    "<td>" + data.list[i].url + "</td>" +
                    "<td>" + data.list[i].nandu + "</td>" +
                    "<td><button onclick='add(this)'>添加到我的文档</button></td>" +
                    "</tr>";
            }
            table.innerHTML=str;
        },
        error:function () {
            alert("查询失败");
        }
    })
});